//
//  ExShareFileViewController.m
//  OC-Example
//
//  Created by a on 16/10/2019.
//  Copyright © 2019 apang.men. All rights reserved.
//

#import "ExShareFileViewController.h"

@interface ExShareFileViewController ()

@end

@implementation ExShareFileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.title = @"文件分享测试";
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"分享测试" forState:UIControlStateNormal];
    [self.view addSubview:button];
    
    [button setTranslatesAutoresizingMaskIntoConstraints:NO];
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
    
    [button addTarget:self action:@selector(shareBtn) forControlEvents:UIControlEventTouchUpInside];
    [self createDocumentDirectory];
}

-(void)createDocumentDirectory{
    
    NSArray*paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    
    NSFileManager * fileManager = [NSFileManager defaultManager];
    NSString *path2 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"directoryTwo"];
    
    if(![fileManager fileExistsAtPath:path2]){
        
        [[NSFileManager defaultManager] createDirectoryAtPath:path2 withIntermediateDirectories:YES attributes:nil error:nil];
        
    }
}

- (void)shareBtn {
    NSString * filePath1 = [[NSBundle mainBundle] pathForResource:@"template" ofType:@"band" inDirectory:@"Resource/Audio"];
    NSString * filePath2 = [[NSBundle mainBundle] pathForResource:@"Deutschland" ofType:@"mp3" inDirectory:@"Resource/Audio"];
    
    NSURL * url1 = [NSURL fileURLWithPath:filePath1];
    NSURL * url2 = [NSURL fileURLWithPath:filePath2];
    
    NSArray *activityItems = @[url1,url2];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]
                                            initWithActivityItems:activityItems
                                            applicationActivities:nil];
    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable   activityType,
                                              BOOL completed,
                                              NSArray * _Nullable returnedItems,
                                              NSError * _Nullable activityError) {
        
        NSLog(@"activityType: %@,\n completed: %d,\n returnedItems:%@,\n activityError:%@",activityType,completed,returnedItems,activityError);
    };
    [self presentViewController:activityVC animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
